package cn.com.ddapp.ddm.ui.mine;

import android.content.Intent;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
import com.google.gson.Gson;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;
import butterknife.OnClick;
import cn.com.ddapp.ddm.AppConfig;
import cn.com.ddapp.ddm.R;
import cn.com.ddapp.ddm.adapter.DetailAdapter;
import cn.com.ddapp.ddm.api.OKUtilAPI;
import cn.com.ddapp.ddm.base.BaseActivity;
import cn.com.ddapp.ddm.base.BaseApplication;
import cn.com.ddapp.ddm.bean.mine.MoneyDetail;
import cn.com.ddapp.ddm.inter.OkUtilCallback;
import cn.com.ddapp.ddm.ui.empty.EmptyLayout;
import cn.com.ddapp.ddm.util.NetUtils;
import cn.com.ddapp.ddm.util.SPUtils;
import cn.com.ddapp.ddm.widget.pulltorefresh.PullToRefreshLayout;
import cn.com.ddapp.ddm.widget.pulltorefresh.pullableview.PullableListView;


public class DetailActivity extends BaseActivity {

    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.error_layout)
    EmptyLayout mEmptyLayout;
    @BindView(R.id.layout_refresh_detail)
    PullToRefreshLayout layoutRefresh;
    @BindView(R.id.lv_info)
    PullableListView listView;
    private List<MoneyDetail.ListBean> mList = new ArrayList<MoneyDetail.ListBean>();
    private DetailAdapter adapter;
    private Integer st = 0;//数据起始
    String userId;

    @Override
    public void initView() {
        setTitle("钱包");
        userId = SPUtils.getStringData(this, SPUtils.USERID);
        st = 0;
        requestData(0);
        setListener();
    }

    @Override
    public void initData() {}

    private void setListener() {
        //上下拉刷新监听
        layoutRefresh.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {
            //上拉刷新
            @Override
            public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
                st = 0;//设置页码为0
                requestData(st);
            }

            //下拉加载
            @Override
            public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
                requestLoadData(st);
            }
        });
    }

    private void requestData(final int s) {
        if (!NetUtils.isConnected(this)) {
            Toast.makeText(this, "请检查你的网络", Toast.LENGTH_SHORT).show();
        } else {
            mEmptyLayout.setErrorType(EmptyLayout.NETWORK_LOADING);
            OkUtilCallback callback = new OkUtilCallback(mEmptyLayout) {
                @Override
                public void onMyResponse(String response, int id) {
                    Gson gson = new Gson();
                    MoneyDetail detail = gson.fromJson(response, MoneyDetail.class);
                    if ((!AppConfig.RESULT_OK.equalsIgnoreCase(detail.getResult())) && detail.getLogin()) {
                        BaseApplication.getIntstance().showToast("请检查网络", Toast.LENGTH_SHORT, 0, Gravity.CENTER);
                    } else if ((!AppConfig.RESULT_OK.equalsIgnoreCase(detail.getResult()))) {
                        BaseApplication.getIntstance().showToast(detail.getResult(), Toast.LENGTH_SHORT, 0, Gravity.TOP);
                    }  else if ((AppConfig.RESULT_OK.equalsIgnoreCase(detail.getResult()))) {
                        if (detail.getList().size() == 0){
                            mEmptyLayout.setErrorType(EmptyLayout.NODATA);
                        }else {
                            mList = detail.getList();
                            st = s + mList.size();
                            adapter = new DetailAdapter(DetailActivity.this, mList);
                            listView.setAdapter(adapter);
//                            adapter.notifyDataSetChanged();
                            layoutRefresh.refreshFinish(PullToRefreshLayout.SUCCEED);
                        }
                    }
                }
            };
            OKUtilAPI.post(OKUtilAPI.moneyDetail, "userId=" + userId + "&st=" + s, callback);
        }
    }

    private void requestLoadData(final int s) {
        if (!NetUtils.isConnected(this)) {
            Toast.makeText(this, "请检查你的网络", Toast.LENGTH_SHORT).show();
        } else {
            mEmptyLayout.setErrorType(EmptyLayout.NETWORK_LOADING);
            OkUtilCallback okUtilCallback = new OkUtilCallback(mEmptyLayout) {
                @Override
                public void onMyResponse(String response, int id) {
                    Gson gson = new Gson();
                    MoneyDetail bean = gson.fromJson(response, MoneyDetail.class);
                    if ((!AppConfig.RESULT_OK.equalsIgnoreCase(bean.getResult()))) {
                        BaseApplication.getIntstance().showToast(bean.getResult(), Toast.LENGTH_SHORT, 0, Gravity.TOP);
                    } else if ((AppConfig.RESULT_OK.equalsIgnoreCase(bean.getResult()))) {
                        List<MoneyDetail.ListBean> sList = bean.getList();
                        if (sList.size() > 0) {
                            mList.addAll(sList);
                            adapter.notifyDataSetChanged();
                            layoutRefresh.refreshFinish(PullToRefreshLayout.SUCCEED);
                            st = s + sList.size();
                        } else {
                            layoutRefresh.loadmoreFinish(PullToRefreshLayout.NODATA);
                        }
                    }
                }
            };
            OKUtilAPI.post(OKUtilAPI.moneyDetail, "userId=" + userId + "&st=" + s, okUtilCallback);
        }
    }

    @OnClick({R.id.img_error_layout,R.id.back})
    public void onClick(View v) {
        Intent intent;
        switch (v.getId()) {
            case R.id.img_error_layout:
                requestData(0);
                break;
            case R.id.back:
                finish();
                break;
        }
    }

    @Override
    public int getContentViewId() {
        return R.layout.activity_detail;
    }

    @Override
    protected String getFragmentTag() {
        return null;
    }
}
